package com.MenuFunction;

import com.WindowFunction.CreateWindowAction;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;


public class CreateMenu {
    /**
     * 创建菜单项页面
     *
     * @param e
     */
    public void CreateRightClickMenu(JFrame frame, MouseEvent e) {
        JPopupMenu jPopupMenu = new JPopupMenu();
        ModifyMenuItemUIAction itemUIAction = new ModifyMenuItemUIAction();
        // 创建菜单项1
        JMenuItem menuItem1 = itemUIAction.ModifyMenuItemUI("XXXXX", 200, 30, 20f);
        menuItem1.addActionListener(e1 -> {
            System.out.println("点击了菜单1");
        });
        // 创建菜单项2
        JMenuItem menuItem2 = itemUIAction.ModifyMenuItemUI("结束", 200, 30, 20f);
        menuItem2.addActionListener(e2 -> {
            System.exit(0);
        });
        //创建菜单项3
        JMenuItem menuItem3 = itemUIAction.ModifyMenuItemUI("修改窗口大小", 200, 30, 20f);
        menuItem3.addActionListener(e3 -> {
            frame.setAlwaysOnTop(false);
            JFrame frame1 = new CreateWindowAction().CreateWindow(true, true, 200, 200,
                    new Color(0, 0, 0, 225), JFrame.HIDE_ON_CLOSE, true);
            CreateComponents(frame1, frame);
        });
        //创建菜单项4

        // 将菜单项添加到菜单中
        jPopupMenu.add(menuItem1);
        jPopupMenu.add(menuItem2);
        jPopupMenu.add(menuItem3);
        //显示在画布中
        jPopupMenu.show(e.getComponent(), e.getX(), e.getY());
    }

    /**
     * 创建修改窗口页面
     * @param newframe
     * @param oldfrme
     */
    public static void CreateComponents(JFrame newframe, JFrame oldfrme) {
        newframe.setAlwaysOnTop(true);
        JPanel jPanel = new JPanel();                       //创建布局
        jPanel.setLayout(new FlowLayout());                 //设置线性布局
        ModifyMenuItemUIAction modifyMenuItemUIAction = new ModifyMenuItemUIAction();

        JTextField textField_width = modifyMenuItemUIAction.ModifyWindowComponents(jPanel, "宽：", 13, String.valueOf(oldfrme.getWidth()));
        JTextField textField_height = modifyMenuItemUIAction.ModifyWindowComponents(jPanel, "高：", 13, String.valueOf(oldfrme.getHeight()));


        JButton jButton = new JButton("保存");
        jPanel.add(jButton);

        JButton jButton1 = new JButton("取消");
        jPanel.add(jButton1);

        newframe.getContentPane().add(jPanel);  //把组件放置到新画布中
        
        jButton.addActionListener(e -> {            //监听保存按钮
            oldfrme.setSize(Integer.valueOf(textField_width.getText()),Integer.valueOf(textField_height.getText()));
            oldfrme.setLocationRelativeTo(null);
            newframe.dispose();
            oldfrme.setAlwaysOnTop(true);
        }
        );

        jButton1.addActionListener(e -> {       //监听取消按钮
            newframe.dispose();
            oldfrme.setAlwaysOnTop(true);
        });


    }

}
